package com.wanghlme.leetcodecn;

import java.util.Stack;

/**
 * <p>剑指 Offer 30. 包含min函数的栈</p>
 * <p>JZ30->main0155</p>
 *
 * @author wanghlme
 * @version 1.0
 */
public class LC_JZ30 {
    public static void main(String[] args) {

    }

    /**
     * Your MinStack object will be instantiated and called as such:
     * MinStack obj = new MinStack();
     * obj.push(x);
     * obj.pop();
     * int param_3 = obj.top();
     * int param_4 = obj.min();
     */

    class MinStack {
        Stack<Integer> stack1;
        Stack<Integer> stack2;

        /**
         * initialize your data structure here.
         */
        public MinStack() {
            stack1 = new Stack<>();
            stack2 = new Stack<>();
        }

        public void push(int x) {
            stack1.add(x);
            if (stack2.empty() || stack2.peek() >= x) {
                stack2.add(x);
            }
        }

        public void pop() {
            if (stack1.pop().equals(stack2.peek())) {
                stack2.pop();
            }
        }

        public int top() {
            return stack1.peek();
        }

        public int min() {
            return stack2.peek();
        }
    }
}